# -*- coding: utf-8 -*-
#!/usr/bin/env python


import urllib2, urllib, cookielib, re, time

def login():
    ''' 登录paiq '''
	casurl = "http://cas.erp.sina.com.cn/cas/login?service=http://10.79.32.160:8280/asis/login&ext=aHR0cDovLzEwLjc5LjMyLjE2MDo4MjgwL2FzaXMv"
	asisurl = "http://10.79.32.160:8280/asis/login"
	#设置一个cookie处理器，它负责从服务器下载cookie到本地，并且在发送请求时带上本地的cookie 
	cj = cookielib.LWPCookieJar()
	cookie_support = urllib2.HTTPCookieProcessor(cj) 
	opener = urllib2.build_opener(cookie_support, urllib2.HTTPHandler)  
	urllib2.install_opener(opener)

	print "start>>>"
	#构造header，一般header至少要包含一下两项。这两项是从抓到的包里分析得出的。 
	headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36',}
	#构造Post数据，他也是从抓大的包里分析得出的
	username = "12624"
	password = "mwk813111"
	#需要给Post数据编码
	caspostData = urllib.urlencode({'lt':"LT-1252207-A5Plvf8bxZyGO6oRuiSK",
				     	'ext':"aHR0cDovLzEwLjc5LjMyLjE2MDo4MjgwL2FzaXMv",	
					'username':username,
					'password':password,
				   	})
	asispostDate = urllib.urlencode({'ticket':"ST-2296332-SNgmeH2rdrtGx5sx8kSj",
					'ext':"aHR0cDovLzEwLjc5LjMyLjE2MDo4MjgwL2FzaXMv",
					})
	#通过urllib2提供的request方法来向指定Url发送我们构造的数据，并完成登录过程
	#casloginreq = urllib2.Request(casurl, caspostData, headers)
	asisloginreq = urllib2.Request(asisurl, asispostDate, headers)
	print "<<<<<<<<<<<<<< request >>>>>>>>>>>>>>>>>"
 	##print casloginreq
	print asisloginreq
	print "<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>"
	#casloginrsp = urllib2.urlopen(casloginreq).read()
	asisloginrsp = urllib2.urlopen(asisloginreq).read()
	#print casloginrsp
	print asisloginrsp	
		
def schedulereq():
	scheduleurl = "http://10.79.32.160:8280/asis/action/brief!createSchedule.action";

	post_data = urllib.urlencode({
    		"name":"全选性能test001",
    		"portalBudget":"1000000",
    		"videoBudget":"200000",
    		"portalVideoAttachRatio":"2",
    		"wapBudget":"1000000",
    		"wapAttachRatio":"0",
    		"beginDate":"20140324",
    		"endDate":"20140430",
    		"customerId":"addgs",
    		"industryId":"1",
    		"workDay":"1",
    		"stressRatio":"0",
    		"cpmMax":"",
    		"cpcMax":"",
    		"portalCpd":"1",
    		"portalCpm":"1",
    		"portalAreaMin":"",
    		"mutexPage":"1",
    		"grade":"0",
    		"portalProductChannels":"2,3,4,6,7,8,9,10,12,13,14,16,18,19,20,21,22,23,24,28,29,30,31,33,34,35,36,38,39,40,42,44,48,49,50,54,58",
    		"portalProductTypes":"10,11,12,13,15,16,17,18,21,22,23,24,25,26,27,28,29,32,33,34,35,36,37,38,39,43,44,45,46,47,48",
    		"videoProductTypes":"1,2,3,4,5,6,7,8,9",
    		"wapProductChannels":"5,11,15,17,25,26,27,32,37,41,43,45,46,47,51,52,53,55,56,57,59",
    		"wapProductTypes":"14,19,20,30,31,40,41,42",
    		"portalProductTargetings":"",
    		"stressDays":"",
    		"includeProducts":"",
    		"mutexSchedules":"",
    		"includeScheduleJson":"",
    		"excludeProducts":"",
	});
	req = urllib2.Request(scheduleurl, post_data)
	print "<<<<<<<<<<<<<< schedule request >>>>>>>>>>>>>>>>>"
        print req
        print "<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>"
        rsp = urllib2.urlopen(req).read()
        print rsp

login()	
schedulereq()
